forum

Home / DeveloperSection / Forums / Why I am getting null pointer exception

Why I am getting null pointer exception

Anonymous User 2122 12-Nov-2014
package readandcopyupdate1;
import java.util.ArrayList;
import java.io.*;
public class ReadAndCopyUpdate1{
public static void main(String[] args){
    ReadAndCopyUpdate1 rc = new ReadAndCopyUpdate1();
    final File folder1 = new File("/root/avatar/default/upload/member");
    final File folder2 = new File("/root/avatar/default/upload/Transfer");
    rc.listFilesForOldFolder(folder1,rc.oldFiles);
    rc.listFilesForNewFolder(folder2,rc.newFiles);
            rc.oldFiles.stream().forEach((oldFile) -> {
                System.out.println(oldFile);
        });
 
    System.out.println("\n\n");
            rc.newFiles.stream().forEach((newFile) -> {
                System.out.println(newFile);
        });
}
private void listFilesForOldFolder(final File folder, ArrayList arrayList) {
    for (final File fileEntry : folder.listFiles()) {
        if (fileEntry.isDirectory()) {
            listFilesForOldFolder(fileEntry, arrayList);
        } else {
            String str = fileEntry.getName();
            if(str.equals("index.html")){
              continue;
            }         
            if(str.charAt(32) == '9'){
                arrayList.add(str);
            }
        }
    }
}
private void listFilesForNewFolder(final File folder, ArrayList arrayList) {
    for (final File fileEntry : folder.listFiles()) {
        if (fileEntry.isDirectory()) {
            listFilesForNewFolder(fileEntry, arrayList);
        } else {
            String str = fileEntry.getName();         
            arrayList.add(str);
        }
    }
}
private final ArrayList<String> oldFiles = new ArrayList<>(5);
private final ArrayList<String> newFiles = new ArrayList<>(5);
}

These method used to work in my first version without any problem and now they are suddenly throwing null pointer exception when I am rewriting the code and was doing unit testing for these methods.

Exception in thread "main" java.lang.NullPointerException    at readandcopyupdate1.ReadAndCopyUpdate1.listFilesForOldFolder(ReadAndCopyUpdate1.java:33) at readandcopyupdate1.ReadAndCopyUpdate1.main(ReadAndCopyUpdate1.java:15)

What I am trying to accomplish is the following: 1. folder1 consist of files which have 33 characters in their file name. I want to pick all those files which have number 9 in their 33th position.

1-I'll store all such file names in an arrayList 'oldFiles'

2-I'll scan folder2 and store all files there in arrayList 'newFiles'

3-I am testing them by printing arrayLists values on sceen.


Updated on 13-Nov-2014
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By